<template>
  <el-dialog
    :visible="showDialog"
    :title="form.id ? '修改用户' : '添加用户'"
    @close="onCancel"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="form.username"
          placeholder="请输入用户名"
          :disabled="Boolean(form.id)"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="!form.id">
        <el-input v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row type="flex" justify="end">
        <el-button size="small" @click="onCancel">取消</el-button>
        <el-button size="small" type="primary" @click="onEDitConfirm" v-if="form.id">确定</el-button>
        <el-button size="small" type="primary" @click="onConfirm" v-else>确定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addUser, editUser } from '@/api/users'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名' }],
        password: [{ required: true, message: '请输入密码' }],
        email: [
          { required: true, message: '请输入邮箱' },
          {
            pattern:
              /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: '邮箱格式不正确',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '请输入手机号' },
          {
            pattern:
              /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
            message: '手机号不正确',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    onCancel () {
      this.form = {
        username: '',
        password: '',
        email: '',
        mobile: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    async onConfirm () {
      try {
        await this.$refs.form.validate()
        await addUser(this.form)
        this.$message.success('添加成功')
        this.$emit('update:showDialog', false)
        this.$parent.getUsers()
      } catch (err) {
        console.log(err)
      }
    },
    async onEDitConfirm () {
      let isOk = true
      this.$refs.form.validateField(['email', 'mobile'], (err) => {
        if (err) {
          isOk = false
        }
      })
      if (isOk) {
        await editUser(this.form)
        this.$message.success('修改成功')
        this.$emit('update:showDialog', false)
        this.$parent.getUsers()
      }
    }
  }
}
</script>

<style>
</style>
